class QuotesController < ApplicationController
  before_filter :login_required, :authorization_required, :find_customer
  
  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :put, :only => [ :update ], :redirect_to => { :action => :index }
  verify :method => :post, :only => [ :create ], :redirect_to => { :action => :index }
  verify :method => :delete, :only => [ :destroy ], :redirect_to => { :action => :index }
  
  def index
    if !@customer.nil?
      @quotes = @customer.quotes
    else
      @quotes = Quote.find(:all, :order => 'quote_number DESC')
    end
    respond_to do |format|
      format.html {render :action => 'list.rhtml'}
      format.js {render :action => 'list.rjs'}
      format.xml {render :xml => @quotes.to_xml}
    end
  end
  
  def show
    @quote = Quote.find(params[:id])
  end
  
  def new
    @quote = Quote.new
    respond_to do |format|
      format.html {render :action => 'new.rhtml'}
      format.js {render :action => 'new.rjs'}
    end
  end
  
  def select_resource
    @quote = Quote.find(params[:id])
    @customer = @quote.customer
    @resources = Employee.find(:all) - @quote.resources
    respond_to do |format|
      format.html {render :action => "select_resource.rhtml"}
      format.js {render :action => "select_resource.rjs"}
    end
  end
  
  def add_resource
    @quote = Quote.find(params[:id])
    @customer = @quote.customer
    resource = Employee.find(params[:resource][:employee_id])
    @quote.resources << resource
    respond_to do |format|
      format.html {redirect_to :controller => 'customers', :action => 'show', :id => @customer}
      format.js {render :action => 'list.rjs'}
    end
  end
  
  def remove_resource
    @quote = Quote.find(params[:id])
    @customer = @quote.customer
    resource = Employee.find(params[:resource])
    @quote.resources.delete(resource)
    respond_to do |format|
      format.html {redirect_to :controller => 'customers', :action => 'show', :id => @customer}
      format.js {render :action => 'list.rjs'}
    end
  end
  
  def new_document
    @quote = Quote.find(params[:id])
    @customer = @quote.customer
    respond_to do |format|
      format.html {render :action => "new_document.rhtml"}
      format.js {render :action => "new_document.rjs"}
    end
    
  end
  
  def create_document
    @quote = Quote.find(params[:id])
    @customer = @quote.customer
    document = Document.new(params[:document])
    @quote.documents << document
    if @quote.save
      responds_to_parent do
        render :action => 'list.rjs'
      end
    else
      render :action => "new_document" unless request.xhr?
    end
  end
  
  def remove_document
    @quote = Quote.find(params[:id])
    @customer = @quote.customer
    document = Document.find(params[:document])
    @quote.documents.delete(document)
    respond_to do |format|
      format.html {redirect_to :controller => 'customers', :action => 'show', :id => @customer}
      format.js {render :action => 'list.rjs'}
    end
  end
  
  def create
    quote = Quote.new(params[:quote])
    quote.quote_number = find_quote_number
    @customer.quotes << quote
    if @customer.save
      redirect_to :controller => 'customers', :action => 'show', :id => @customer unless request.xhr?
    else 
      render :action => 'new' unless request.xhr?
    end
  end
  
  def edit
    @quote = Quote.find(params[:id])
    @customer = @quote.customer
    respond_to do |format|
      format.html {render :action => 'edit.rhtml'}
      format.js {render :action => 'edit.rjs'}
    end
  end
  
  def update
    @quote = Quote.find(params[:id])
    @customer = @quote.customer
    if @quote.update_attributes(params[:quote])
      respond_to do |format|
        format.html {redirect_to :controller => 'customers', :action => 'show', :id => @customer}
        format.js {render :action => 'list.rjs'}
      end
    else
      render :action => 'edit' unless request.xhr?
    end
    
    
  end
  
  def destroy
    Quote.find(params[:id]).destroy
    index
  end
  
  def destroy_selected
    unless params[:selected].nil?
      params[:selected].each do |id|
        quote = Quote.find(id)
        @customer = quote.customer
        quote.destroy
      end
    end
    respond_to do |format|
      format.html {redirect_to :controller => 'customers', :action => 'show', :id => @customer}
      format.js {render :action => 'list.rjs'}
    end
  end

  private

  def find_quote_number
    i = 1
    quote_number_prefix = "T#{Time.now.strftime("%y%m")[1, 3]}"
    quote_number = quote_number_prefix + "%02d" % i
    until Quote.find(:first, :conditions => ["quote_number = ?", quote_number]).nil? do
      i += 1
      quote_number = quote_number_prefix + "%02d" % i
    end
    quote_number
  end
end
